﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;
using System.Text.RegularExpressions;

namespace mamnontuoitho7.module
{
    public partial class TeacherWarning : System.Web.UI.Page
    {
        protected Notice _notice = new Notice();
        protected Class _class = new Class();
        protected General _general = new General();

        protected DataTable _dt;
        protected String _className = "";
        protected int _pageflag;
        protected int _currentbar = 1;

        protected int _totalPages;
        protected int _totalConversationNumber;
        protected int _totalBars;

        protected int _beginPage;// dung bên trang giao diện
        protected int _endPage;// dung bên trang giao diện

        protected void Page_Load(object sender, EventArgs e)
        {

            //Check Permission
            if (Session["memberID"].ToString() != "" && Session["role"].ToString() == "3")
            {
                if (Request.QueryString["NoticeIDDelete"] != null)
                {
                    string temp2 = Request.QueryString["NoticeIDDelete"].ToString();
                    Match match2 = Regex.Match(temp2, @"([0-9])$", RegexOptions.IgnoreCase);

                    if (temp2.Length <= 4 && match2.Success)
                    {
                        string _NoticeID = Request.QueryString["NoticeIDDelete"].ToString();

                        bool check = false;
                        try
                        {
                            check = _notice.checkNoticeOwnerForTeacher(Session["memberID"].ToString(), _NoticeID);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            Response.Redirect(ConstantClass._errorLoginPage);
                        }
                        if (check)
                        {
                            _notice.NoticeDelete(_NoticeID);
                        }
                    }
                    else
                    {
                        Response.Redirect("Home.aspx");
                    }
                }

                try
                {
                    _className = _class.GetClassNameByTeacherID(Session["memberID"].ToString())["ClassName"].ToString();
                    ////////////////////start paging
                    _totalConversationNumber = _notice.TotalNoticeOfOneClass(Session["classID"].ToString()).GetValueOrDefault();
                    _totalPages = _general.Divide(_totalConversationNumber, ConstantClass._pageStepConversation);
                    _totalBars = _general.Divide(_totalPages, ConstantClass._barStepConversation);

                    if (Request.QueryString["page"] != null)// khi bấm vào các ô số
                    {
                        string temp = Request.QueryString["page"].ToString();
                        Match match = Regex.Match(temp, @"([0-9])$", RegexOptions.IgnoreCase);

                        if (temp.Length <= 4 && match.Success)
                        {
                            _pageflag = Convert.ToInt16(temp);// trang hiện hành
                            if (_pageflag <= _totalPages)
                            {
                                _currentbar = _general.Divide(_pageflag, ConstantClass._barStepConversation);
                                Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                            }
                            else
                            {
                                _pageflag = 1;
                                _currentbar = 1;
                                Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                            }
                        }
                    }
                    if (Request.QueryString["page"] == null && Request.QueryString["a"] == null)// khi mới vào trang
                    {
                        _currentbar = 1;
                        _pageflag = 1;
                        Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                    }

                    if (Request.QueryString["a"] != null && Request.QueryString["a"] == "1") // khi bấm vào ô >> (next)
                    {
                        _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                        if (_currentbar < _totalBars)
                        {
                            _currentbar += 1;
                            Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                        }
                        _pageflag = (_currentbar - 1) * ConstantClass._barStepConversation + 1;

                    }

                    if (Request.QueryString["a"] != null && Request.QueryString["a"] == "-1") // khi bấm vào ô << (previous)
                    {
                        _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                        if (_currentbar > 1)
                        {
                            _currentbar -= 1;
                            Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                        }
                        _pageflag = (_currentbar - 1) * ConstantClass._barStepConversation + 1;
                    }

                    _dt = _notice.GetNoticePaged(_pageflag, ConstantClass._pageStepConversation, Session["classID"].ToString());
                    _beginPage = (_currentbar - 1) * ConstantClass._barStepConversation + 1;
                    _endPage = _currentbar * ConstantClass._barStepConversation;

                    if (_endPage > _totalPages)
                    {
                        _endPage = _totalPages;
                    }
                    //////////////////////////////end
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorLoginPage);
                }
            }
            else
            {
                Response.Redirect("Home.aspx");
            }


            Session["Common_articleTypeID"] = "1";

        }

        protected void btnWarningSend_Click(object sender, EventArgs e)
        {

            if (ckeWarning.Text.Length < 23)
            {
                Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script language=javascript>alert('Nội dung thông báo phải nhiều hơn 10 ký tự.')</script>");
            }
            else if (ckeWarning.Text.Length > 3500)
            {
                Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script language=javascript>alert('Nội dung thông báo quá giới hạn 3500 ký tự.')</script>");
            }
            else
            {
                try
                {
                    _notice.NoticeCreate(Session["classID"].ToString(), Session["memberID"].ToString(), ckeWarning.Text.Trim());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorLoginPage);
                }
                ckeWarning.Text = "";
                Response.Redirect("TeacherWarning.aspx");
            }

        }
    }
}